define(['zepto', 'butterfly/view','../../bsl',
    'text!./barcode.html'
], function($, View, bsl, barcode) {
    var me = null;

    var view = View.extend({
        events: {

            // ！！！！ 由于 引入 touch.js 后，tap事件会变成双击，所以 本页面内不能使用tap，用click代替。
               
                "tap .onempty":"onempty",
                "tap .cleanHistroy":"cleanHistroy",
                "tap #openDoc" : "openDoc",
                "tap #back" : "back",
                "tap #clicked" : "clicked",
                
     
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等           


        },
         img : null,
        blist : [],
         scaned:function ( t, r, f ) {
            var me=this;
            var d = new Date();
            var h=d.getHours(),m=d.getMinutes(),s=d.getSeconds(),ms=d.getMilliseconds();
            if ( h < 10 ) { h='0'+h; }
            if ( m < 10 ) { m='0'+m; }
            if ( s < 10 ) { s='0'+s; }
            if ( ms < 10 ) { ms='00'+ms; } else if ( ms < 100 ) { ms='0'+ms; }
            var ts = '['+h+':'+m+':'+s+'.'+ms+']';
            var li=null,hl = document.getElementById("history");
            if ( blist.length > 0 ) {
                li = document.createElement("li");
                li.className = "ditem";
                hl.insertBefore( li, hl.childNodes[0] );
            } else {
                li = document.getElementById("nohistory");
            }
            li.id = blist.length;
            var html = '['+h+':'+m+':'+s+'.'+ms+']'+'　　'+t+'码<div class="hdata">';
            html += r;
            html += '</div>';
            li.innerHTML = html;
            li.setAttribute( "onclick", "selected(id);" );
            blist[blist.length] = {type:t,result:r,file:f};
            me.update( t, r, f );
        },
         selected:function ( id ) {
            var h = blist[id];
            update( h.type, h.result, h.file );
            if ( h.result.indexOf("http://")==0  || h.result.indexOf("https://")==0 ) {
                bsl.nativeUI.confirm( h.result, function ( i ) {
                    if ( i.index == 0 ) {
                        bsl.runtime.openURL( h.result );
                    }
                }, "", ["打开","取消"] );
            } else {
                bsl.nativeUI.alert( h.result );
            }
        },
         update:function ( t, r, f ) {
            console.info( "扫描成功：");
            console.info( t );
            console.info( r );
            console.info( "\n图片地址："+f );
            if ( !f || f=="null" ) {
                img.src = "../images/barcode.png"; 
            } else {
                bsl.io.resolveLocalFileSystemURL(f,function(entry){
                    img.src=entry.toLocalURL();
                });
                //img.src = "http://localhost:13131/"+f;
            }
        },
         onempty:function () {
            if ( window.plus ) {
                bsl.nativeUI.alert('无扫描记录');
            } else {
                alert( '无扫描记录' );
            }
        },
        openDoc:function(){
             window.butterfly.navigate('com.no5.cordovademo/doc/barcode', {
                trigger: true
            });
        },
         cleanHistroy:function () {
            if ( blist.length > 0 ) {
                var hl = document.getElementById("history");
                hl.innerHTML = '<li id="nohistory" class="ditem" onclick="onempty();">无历史记录 </li>';
            }
            bsl.io.resolveLocalFileSystemURL( "_doc/barcode/", function ( entry ) {
                entry.removeRecursively( function () {
                    // Success
                }, function ( e ) {
                    //alert( "failed"+e.message );
                });
            } );
        },
         back : function(e){
            window.history.go(-1);
        },
         clicked : function(e){
           
            window.butterfly.navigate('com.no5.cordovademo/page/barcode_scan', {
                trigger: true
            });
        },
               
            });
            return view;
        });
